package com.hk.housekeeping.controller;

import com.hk.housekeeping.entity.OrderItem;
import com.hk.housekeeping.entity.RespItemDetail;
import com.hk.housekeeping.service.ItemServ;
import com.hk.housekeeping.utils.PageUtils;
import com.hk.housekeeping.utils.ResponseCode;
import com.hk.housekeeping.utils.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/item")
public class ItemController {
    @Autowired
    private ItemServ itemServ;

    //分页查询
    @GetMapping("/pageSelect")
    public  ResponseEntity<PageUtils<List<OrderItem>>> pageSelect(@RequestParam(value = "page", defaultValue = "1") int currentPage, @RequestParam(value = "size", defaultValue = "5") int pageSize) {
        ResponseEntity<PageUtils<List<OrderItem>>> entity = null;

        try {
            entity = itemServ.pageSelect(currentPage, pageSize);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.ITEM_PAGE_SELECT_ERROR);
        }

        return entity;
    }

    //删除一条项目记录（假删除）
    @DeleteMapping("/delItem")
    public ResponseEntity<Integer> deleteItem(@RequestParam int id) {
        ResponseEntity<Integer> entity = null;

        try {
            entity = itemServ.deleteItem(id);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.ITEM_DELETE_ERROR);
        }
        return entity;
    }

    //根据项目名称进行查找（模糊查找）
    @GetMapping("/searchByName")
    public ResponseEntity<PageUtils<List<OrderItem>>> searchItemByName(@RequestParam String name, @RequestParam(value = "page", defaultValue = "1") int currentPage, @RequestParam(value = "size", defaultValue = "5") int pageSize) {
        ResponseEntity<PageUtils<List<OrderItem>>> entity = null;
        try {
            entity = itemServ.searchByName(name, currentPage, pageSize);
        }catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.ITEM_SEARCH_BY_NAME_ERROR);
        }
        return entity;
    }

    //根据有效日期进行查找
    @GetMapping("/searchByTime")
    public ResponseEntity<PageUtils<List<OrderItem>>> searchItemByTime(@RequestParam String startTime, @RequestParam String endTime, @RequestParam(value = "page", defaultValue = "1") int currentPage, @RequestParam(value = "size", defaultValue = "5") int pageSize) {
        ResponseEntity<PageUtils<List<OrderItem>>> entity = null;
        try {
            entity = itemServ.searchByTime(startTime, endTime, currentPage, pageSize);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.ITEM_SEARCH_BY_TIME_ERROR);
        }

        return entity;
    }

    //查看详情（返回项目详情表数据，参数传递Order_item的id）
    @GetMapping("/itemDetail")
    public ResponseEntity<RespItemDetail> detailView(@RequestParam("id") int itemId) {
        ResponseEntity<RespItemDetail> entity = null;
        try {
            entity = itemServ.detailView(itemId);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.ITEM_DETAIL_SELECT_ERROR);
        }

        return entity;
    }

    //修改项目详情
    @PostMapping("/updateDetail")
    public ResponseEntity<Integer> updateItemDetail(@RequestBody RespItemDetail respItemDetail) {
        ResponseEntity<Integer> entity = null;
        try {
            entity = itemServ.updateItemDetail(respItemDetail);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.ITEM_DETAIL_UPDATE_ERROR);
        }

        return entity;
    }

    //新增项目
    @PostMapping("/addItem")
    public ResponseEntity<Integer> addItem(@RequestBody OrderItem orderItem) {
        ResponseEntity<Integer> entity = null;
        try {
            entity = itemServ.addItem(orderItem);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.ITEM_ADD_ERROR);
        }
        return entity;
    }

}
